package com.atguigu.gulimall.authserver.controller;


import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.authserver.service.MemberFeignService;
import com.atguigu.gulimall.authserver.service.ThirdPatFeignService;
import com.atguigu.gulimall.authserver.vo.UserLoginVo;
import com.atguigu.gulimall.authserver.vo.UserRegisterVo;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author 860620068
 * @Date 2021/6/11 :16:31
 * @desceiption:
*/
@Controller
public class LoginController {
    @Autowired
    private ThirdPatFeignService thirdPatFeignService;

    @Autowired
    private MemberFeignService memberFeignService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ResponseBody
    @GetMapping(value = "/sms/sendCode")
    public R sendCode(@RequestParam("phone") String phoen){

        //1.接口防刷
        String redisCode = stringRedisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phoen);
        if(!StringUtils.isEmpty(redisCode)){
            //活动存入redis的时间，用当前时间减去存入redis的时间，判断用户手机是否在60s内发送验证码
            long currentTime = Long.parseLong(redisCode.split("_")[1]);
            if(System.currentTimeMillis() - currentTime <60000){
                // 60s 内不能再发
                return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(),BizCodeEnum.SMS_CODE_EXCEPTION.getMessage());
            }
        }

        //2.验证码的再次校验 redis .存key-phone,value-code
        int code =(int)((Math.random()*9+1)*100000);
        String codeNum = String.valueOf(code);
        String redisStorage = codeNum + "_"+System.currentTimeMillis();

        //存入redis,防止同一个手机号在60秒内再次发送验证码
        stringRedisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phoen,
                redisStorage,10, TimeUnit.MINUTES);
        thirdPatFeignService.sendCode(phoen,codeNum);

        return R.ok();
    }

    /**
     *  TODO:重定向携带数据：利用session原理，将数据放在session中。
     *  TODO:只要跳转到下一个页面取出这个数据以后，session里面的数据就会删掉
     *  TODO:分布下session问题
     *  RedirectAttributes:重定向也可以保留数据，不会丢失
     * @param vos
     * @param result
     * @param attributes
     * @return
     */
    @PostMapping(value = "/register")
    public String register(@Valid UserRegisterVo vos, BindingResult result, RedirectAttributes attributes){
        //如果有错误回到注册页面
        if(result.hasErrors()){
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            attributes.addFlashAttribute("errors",errors);

            //效验出错回到注册页面
            return "redirect:http://auth.gulimall.com/reg.html";
        }

        //1.效验验证码
        String code = vos.getCode();
        //获取存入Redis里的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + vos.getPhone());
        if(!StringUtils.isEmpty(redisCode)){
            //截取字符串
            if(code.equals(redisCode.split("_")[0])){
                //删除验证码；令牌机制
                stringRedisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX+vos.getPhone());
                //验证码通过看，真正注册，调用远程服务进行注册
                R register = memberFeignService.register(vos);
                if(register.getCode()==0){
                    //成功
                    return "redirect:http://auth.gulimall.com/login.html";
                }else {
                    //失败
                    Map<String,String> errors = new HashMap<>();
                    errors.put("msg",register.getData("msg",new TypeReference<String>(){}));
                    attributes.addFlashAttribute("errors",errors);
                    return "redirect:http://auth.gulimall.com/reg.html";
                }
            }else {
                //效验出错回到注册页面
                Map<String,String> errors = new HashMap<>();
                errors.put("code","验证码错误");
                attributes.addFlashAttribute("errors",errors);
                return "redirect:http://auth.gulimall.com/reg.html";
            }
        }else{
            //效验出错回到注册页面
            Map<String,String> errors = new HashMap<>();
            errors.put("code","验证码错误");
            attributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.gulimall.com/reg.html";
        }
    }

    @GetMapping(value="/login.html")
    public String loginPage(HttpSession session){
        //从session先取出来用户的信息，判断用户是否已经登录过了
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
        //如果用户没有登录那就跳转到登录页面
        if(attribute == null){
            return "login";
        }else{
            return "redirect:http://gulimall.com";
        }
   }

   @PostMapping(value = "/login")
   public String login(UserLoginVo vo,RedirectAttributes attributes,HttpSession session){
        //远程登录
       R login = memberFeignService.login(vo);
       if(login.getCode() ==0){
           MemberResponseVo data = login.getData("data", new TypeReference<MemberResponseVo>() {
           });
           session.setAttribute(AuthServerConstant.LOGIN_USER,data);
           return "redirect:http://gulimall.com";
       }else{
           Map<String,String> errors = new HashMap<>();
           errors.put("msg",login.getData("msg",new TypeReference<String>(){}));
           attributes.addFlashAttribute("errors",errors);
           return "redirect:http://auth.gulimall.com/login.html";
       }
   }

   @GetMapping(value = "/logout.html")
   public String logout(HttpServletRequest request){
        request.getSession().removeAttribute(AuthServerConstant.LOGIN_USER);
        request.getSession().invalidate();
        return "redirect:http://gulimall.com";
   }
}





















